def get_last_session(query: dict) -> list:
    return [{'$match': query}, {'$sort': {'updated_at': -1}}, {'$limit': 1}]


def get_session_link_target():
    return [
        {
            '$lookup': {
                'from': 'scan_targets',
                'localField': 'target_id',
                'foreignField': 'target_id',
                'as': 'target',
            }
        }
    ]


def get_task_link_event():
    return [
        {
            '$lookup': {
                'from': 'scan_events',
                'localField': 'last_task_session_id',
                'foreignField': 'task_session_id',
                'as': 'event',
            }
        }
    ]


def get_session_link_event():
    return [
        {
            '$lookup': {
                'from': 'scan_events',
                'localField': 'task_session_id',
                'foreignField': 'task_session_id',
                'as': 'event',
            }
        }
    ]


def get_session_link_extra():
    return [
        {
            '$lookup': {
                'from': 'scan_task_sessions_extra',
                'localField': 'task_session_id',
                'foreignField': 'task_session_id',
                'as': 'extra',
            }
        },
        {'$unwind': {'path': '$extra', 'preserveNullAndEmptyArrays': True}},
    ]
